package com.dusk.net
{
	import com.dusk.game.Logger;
	import com.hurlant.util.Base64;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.net.URLRequestHeader;
	import flash.net.URLRequestMethod;
	import flash.utils.getTimer;
	
	public class ServerConnect
	{
		
		private static const TIME_API_URL:String = "http://save.api.4399.com/?ac=get_time";
		
		private static const account:String = "gameData";
		
		private static const password:String = "UazqcEQ386mZaFTP";
		
		private static var cloudConfig:Object = {
			newVersionId:-1,
			newVersionName:"",
			newVersionDescription:"",
			publishTime:""
		};
		
		private static var serverTime:String = null;
		
		private static var _timer:uint;
		
		public static function initData():void
		{
//			initUpdateData();
//			init4399Data();
			_timer = getTimer();
		}
		
		public static function refreshData():void
		{
			if(getTimer() - _timer > 60 * 1000)
			{
				initData();
			}
		}
		
		public static function getNewVersionId():int
		{
			if(cloudConfig.newVersionId)
			{
				return cloudConfig.newVersionId;
			}
			else
			{
				return -1;
			}
		}
		
		public static function getNewVersionName():String
		{
			if(cloudConfig.newVersionName)
			{
				return cloudConfig.newVersionName;
			}
			else
			{
				return null;
			}
		}
		
		public static function getNewVersionDescription():String
		{
			if(cloudConfig.newVersionDescription)
			{
				return cloudConfig.newVersionDescription;
			}
			else
			{
				return null;
			}
		}
		
		public static function getNewVersionPublishTime():String
		{
			if(cloudConfig.publishTime)
			{
				return cloudConfig.publishTime;
			}
			else
			{
				return null;
			}
		}
		
		public static function getServerDate():String
		{
			if(!serverTime)
			{
				return null;
			}
			return serverTime.split(" ")[0];
		}
		
		public static function getServerTime():String
		{
			if(!serverTime)
			{
				return null;
			}
			return serverTime.split(" ")[1];
		}
		
		private static function initUpdateData():void
		{
			var request:URLRequest;
			request = new URLRequest();
			request.method = URLRequestMethod.GET;
			request.url = WEB_DAV_URL;
			
			/*---------------------webDav需要添加身份header-----------------------*/
			var credentials:String = Base64.encode(account + ":" + password);
			request.requestHeaders.push(new URLRequestHeader("Authorization","Basic " + credentials));
			/*---------------------webDav需要添加身份header-----------------------*/
			
			var uLoader:URLLoader = new URLLoader();
			uLoader.dataFormat = URLLoaderDataFormat.TEXT;
			var delLoader:Function = function():void
			{
				uLoader.removeEventListener(Event.COMPLETE, arguments.callee);
				uLoader.removeEventListener(IOErrorEvent.IO_ERROR, arguments.callee);
				uLoader.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, arguments.callee);
				uLoader.close();
				uLoader = null;
			};
			uLoader.addEventListener(Event.COMPLETE, function (param1:Event):void
			{
				var data:String = uLoader.data;
				delLoader();
				//以下为webDav数据解析
				try{
					cloudConfig = JSON.parse(data);
				}catch (e:Error)
				{
					Logger.log("无法解析更新配置文件，错误信息：" + e.message);
				}
				Logger.log("最新版本号：\n" + getNewVersionId());
				Logger.log("最新版本名称：\n" + getNewVersionName());
				Logger.log("最新版本信息：\n" + getNewVersionDescription());
				Logger.log("发布时间：\n" + getNewVersionPublishTime());
			});
			uLoader.addEventListener(IOErrorEvent.IO_ERROR, function (evt:IOErrorEvent):void
			{
				delLoader();
				Logger.logError("无法从云端获取最新版本信息，io流错误信息：" + evt.text);
			});
			uLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, function (evt:SecurityErrorEvent):void
			{
				delLoader();
				Logger.logError("无法从云端获取最新版本信息，安全错误信息：" + evt.text);
			});
			uLoader.load(request);
		}
		
		private static function init4399Data():void
		{
			var uLoader:URLLoader = new URLLoader();
			uLoader.dataFormat = URLLoaderDataFormat.TEXT;
			var delLoader:Function = function():void
			{
				uLoader.removeEventListener(Event.COMPLETE, arguments.callee);
				uLoader.removeEventListener(IOErrorEvent.IO_ERROR, arguments.callee);
				uLoader.close();
				uLoader = null;
			};
			uLoader.addEventListener(Event.COMPLETE, function (evt:Event):void
			{
				var htmlData:String = uLoader.data;
				delLoader();
				try
				{
					var data:Object = JSON.parse(htmlData);
					serverTime = data["time"];
				}
				catch(e:Error)
				{
					Logger.logError("无法解析网页信息，可能网页结构已经改变");
				}
				Logger.log("网络时间：" + serverTime);
			});
			uLoader.addEventListener(IOErrorEvent.IO_ERROR, function (evt:IOErrorEvent):void
			{
				delLoader();
				Logger.logError("无法从云端获取当前时间，错误信息：" + evt.text);
			});
			uLoader.load(new URLRequest(TIME_API_URL));
		}
		
		private static function get WEB_DAV_URL():String
		{
			CONFIG::DEBUG
			{
				return "https://ogi.teracloud.jp/dav/造梦西游三界纵横/config/debugVersion.json";
			}
			CONFIG::RELEASE
			{
				return "https://ogi.teracloud.jp/dav/造梦西游三界纵横/config/releaseVersion.json";
			}
		}
	}
}
